var ajaxUrl = require('../../utils/url.js');
var encrypt = require('../../utils/encrypt.js');
const app = getApp()
Page({
  data: {
    myCart: [],
    editCart: false,
    category: 0,
    selectAll: false,
    total: {}
  },
  onShow: function (options) {
    const self = this
    self.getCartWithToken()
  },
  setSelect(){
    const self = this
    return self.data.myCart.every(item => {
      return item.selected === true
    })
  },
  setTotal () {
    let total = {price: 0, cartCnt: 0, selected: [], disabled: []}
    this.data.myCart.forEach(p => {
      if (p.selected) {
        total.price += p.price * p.cartCnt
        total.cartCnt += parseInt(p.cartCnt)
        total.selected.push(p.goodsId)
      }
      if (!p.saleStatus) {
        total.disabled.push(p.goodsId)
      }
    })
    return total
  },
  getCartWithToken () {
    const self = this
    let params = {
      token: app.globalData.token
    }
    wx.request({
      method: "POST",
      url: ajaxUrl.cartsList(),
      data: params,
      success: function(res){
        if(res.data.code === 200){
          res.data.data.forEach((item, i) => {
            item.selected = false
          })
          self.setData({
            myCart: res.data.data,
          })
          self.setData({
            selectAll: self.setSelect(),
            total: self.setTotal()
          })
        }else{
          console.error(res.data.msg)
        }
      }
    })
  },
  productCheck (e) {
    let goodsId = e.detail.id
    const self = this
    this.data.myCart.forEach((item, index) => {
      if (item.goodsId === goodsId) {
        // item.selected = !item.selected
        var select = 'myCart['+index+'].selected'
        self.setData({
          [select]: !item.selected
        })
        self.setData({
          selectAll: self.setSelect(),
          total: self.setTotal()
        })
      }
    })
  },
  checkAll (e) {
    let checked = e.detail.checked
    console.log(checked)
    const self = this
    if (typeof checked !== 'boolean') {
      checked = !self.data.selectAll
    }
    if (self.data.total.disabled.length > 0) {
      return
    }
    this.data.myCart.forEach((item, index) => {
      var select = 'myCart['+index+'].selected'
      self.setData({
        [select]: checked
      })
    })
  },
  updateCount (e) {
    const self = this
    let id = e.currentTarget.dataset.id
    let cartCnt = e.currentTarget.dataset.cnt
    if (cartCnt === 0) {
      return
    }
    // self.data.myCart.forEach((item, i) => {
    //   if (item.goodsId === id) {
    //     // item.cartCnt = cartCnt
    //     let str = 'myCart[i].cartCnt'
    //     self.setData({
    //       str: cartCnt
    //     })
    //   }
    // })
    let params = {
      token: app.globalData.token
    }
    wx.request({
      method: "POST",
      url: ajaxUrl.cartsList(),
      data: params,
      success: function(res){
        if(res.data.code === 200){
          let cartParams = []
          res.data.data.forEach(function(item){
            cartParams.push({cnt:item.cartCnt, goodsId:  parseInt(item.goodsId)})
          })
          cartParams = self.addByValue(cartParams, id, cartCnt)
          cartParams = encrypt.encrypt(cartParams)
          wx.request({
            method: "POST",
            url: ajaxUrl.addToCart(),
            data: cartParams,
            success: function(res){
              if(res.data.code === 200){
                self.getCartWithToken()
              }else{
                console.error(res.data.msg)
              }
            }
          })
        }else{
          console.error(res.data.msg)
        }
      }
    })
  },
  removeByValue(arr,value) {
    for(let j=0;j<arr.length;j++){
      if(arr[j].goodsId == value){
        arr.splice(j,1)
        break
      }
    }
    return arr
  },
  addByValue(arr,value, cnt) {
    for(let j=0;j<arr.length;j++){
      if(arr[j].goodsId == value){
        arr[j].cnt = cnt
        break
      }
    }
    return arr
  },
  deleteCart (e) {
    const self = this
    let params = {
      token: app.globalData.token
    }
    let id = e.currentTarget.dataset.id
    let cartParams=[]
    wx.request({
      method: "POST",
      url: ajaxUrl.cartsList(),
      data: params,
      success: function(res){
        if(res.data.code === 200){
          res.data.data.forEach(function(item){
            cartParams.push({cnt:item.cartCnt, goodsId:  parseInt(item.goodsId)})
          })
          cartParams = self.removeByValue(cartParams, id)
          cartParams = encrypt.encrypt(cartParams)
          wx.request({
            method: "POST",
            url: ajaxUrl.addToCart(),
            data: cartParams,
            success: function(res){
              if(res.data.code === 200){
                self.getCartWithToken()
                wx.showToast({
                  title:'删除成功'
                })
              }else{
                console.error(res.data.msg)
              }
            }
          })
        }else{
          console.error(res.data.msg)
        }
      }
    })
  }
})